package com.hxt.controller;

import com.hxt.entity.User;
import com.hxt.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * @Controller 和 @RestController 的区别 (具体：http://blog.csdn.net/gg12365gg/article/details/51345601)
 * @RestController is a stereotype annotation that combines @ResponseBody and @Controller.
 * @RestController 注解相当于@ResponseBody ＋ @Controller合在一起的作用。
 * Created by Huangxiutao on 2017/3/31.
 */
@Controller
@RequestMapping("/hibernate")
@EnableAutoConfiguration
public class HibernateController {
    @Autowired
    private UserRepository userRepository;

    @RequestMapping("getUserById")
    @ResponseBody
    public User getUserById(Long id){
        User u=userRepository.findUserById(id);
        System.out.println(u.toString());
        return u;
    }
    @RequestMapping("saveUser")
    @ResponseBody
    public void saveUser(User user){
        //前台传参：xxxxxx/hibernate/saveUser?userName=hht&passWord=123456&gender=女&address=广东省广州市
//        User u=new User();
//        u.setAddress("广东省深圳市南山区");
//        u.setBirthDate(new Date());
//        u.setGender("男");
//        u.setPassWord("123456");
//        u.setUserName("hxt");
        User u=user;
        userRepository.save(u);
    }
    @RequestMapping("getUserByUserName")
    @ResponseBody
    public User getUserByUserName(String userName){
        User u= userRepository.findByUserName(userName);
        return u;
    }

}
